@tool
class_name Enemy
extends Character



@export var hurt_box: Area2D
@export var health_bar: ProgressBar
@export var health_lbl: Label

@export var circle_color: Color = Color.RED:
     set(value):
        circle_color = value
        queue_redraw()

@export var circle_radius: float = 7.5:
    set(value):
        circle_radius = value
        queue_redraw()

@export var collision_damage: int = 10

@onready var player: Player = get_tree().get_first_node_in_group("player")



func _ready() -> void:
    if Engine.is_editor_hint(): return
    hurt_box.body_entered.connect(_on_hurt_box_body_entered)
    health_changed.connect(
        func():
            health_bar.value = health
            health_lbl.text = str(health)
    )
    
    max_health_changed.connect(
        func():
            health_bar.max_value = max_health
    )
    
    died.connect(
        func():
            player.score += 5
            player.kill_count += 1
            queue_free()
    )
    
    health_bar.max_value = max_health
    health_bar.value = health
    health_lbl.text = str(health)


func _physics_process(delta: float) -> void:
    if Engine.is_editor_hint(): return
    var direction := global_position.direction_to(player.global_position)
    velocity = velocity.move_toward(direction * speed, friction * delta)
    move_and_slide()


func _draw() -> void:
    super._draw()
    draw_circle(Vector2.ZERO, circle_radius, circle_color)


func _on_hurt_box_body_entered(body: Node2D) -> void:
    if body is Bullet and not body.is_handled:
        body.is_handled = true
        position += body.direction * body.knockback_force
        health -= body.damage
#        ObjPool.idle(body)
        body.queue_free()
        
    if body is Player:
        body.health -= collision_damage
        health = 0


func setup(health_: int, speed_: int) -> void:
    max_health = health_
    health = health_
    speed = speed_
